﻿using Autodesk.AutoCAD.Geometry;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CadDotNetTools
{
    public static class PointTools
    {
        public static Point2d Point3dToPoint2d(this Point3d point3d) => new Point2d(point3d.X, point3d.Y);
        public static Point3d Point2dToPoint3d(this Point2d point2d) => new Point3d(point2d.X, point2d.Y, 0);
        public static Point2d GetPoint2dCenter(this Point2d point2d) => new Point2d(point2d.X, point2d.Y) / 2;
        public static Point3d GetPoint3dCenter(this Point3d point3d) => new Point3d(point3d.X, point3d.Y, 0) / 2;

        public static Point3d GetTwoPoint3dCenter(this Point3d start, Point3d end) => new Point3d(start.X + end.X, start.Y + end.Y, 0) / 2;
        public static Point2d GetTwoPoint2dCenter(this Point2d start, Point2d end) => new Point2d(start.X + end.X, start.Y + end.Y) / 2;
        /// <summary>
        /// 获取与给定点指定角度和距离的点
        /// </summary>
        /// <param name="point"></param>
        /// <param name="angle"></param>
        /// <param name="dist"></param>
        /// <returns></returns>
        public static Point3d PolarPoint(this Point3d point, double angle, double dist) => new Point3d(point.X + dist * Math.Cos(angle), point.Y + dist * Math.Sin(angle), point.Z);

        /// <summary>
        /// 判断三点是否在同一条直线上
        /// </summary>
        /// <param name="firstPoint">第一个点</param>
        /// <param name="secondPoint">第二个点</param>
        /// <param name="thirdPoint">第三个点</param>
        /// <returns></returns>
        public static bool IsOnOneLine(this Point3d firstPoint, Point3d secondPoint, Point3d thirdPoint)
        {
            Vector3d v21 = secondPoint.GetVectorTo(firstPoint);
            Vector3d v23 = secondPoint.GetVectorTo(thirdPoint);
            if (v21.GetAngleTo(v23) == 0 || v21.GetAngleTo(v23) == Math.PI)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 获得point X 方向与point2的角度
        /// </summary>
        /// <param name="point1">点1</param>
        /// <param name="point2">点2</param>
        /// <returns></returns>
        public static double GetAngleToXAxis(this Point3d point1, Point3d point2)
        {
            var vector1 = point1.GetVectorTo(point1 + Vector3d.XAxis * 1);
            var vector2 = point1.GetVectorTo(point2);
            return (vector1.GetAngleTo(vector2)).DegreeToAngle();
        }

    }
}
